package com.yic.module.base.controller.admin.centeruser;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.api.dept.DeptApi;
import com.yic.module.system.api.dept.dto.DeptRespDTO;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.centeruser.vo.*;
import com.yic.module.base.dal.dataobject.centeruser.CenterUserDO;
import com.yic.module.base.convert.centeruser.CenterUserConvert;
import com.yic.module.base.service.centeruser.CenterUserService;

@Tag(name = "管理后台 - 工作中心人员绑定")
@RestController
@RequestMapping("/base/center-user")
@Validated
public class CenterUserController {

    @Resource
    private CenterUserService centerUserService;
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建工作中心人员绑定")
    @PreAuthorize("@ss.hasPermission('base:center-user:create')")
    public CommonResult<Long> createCenterUser(@Valid @RequestBody CenterUserCreateReqVO createReqVO) {
        return success(centerUserService.createCenterUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工作中心人员绑定")
    @PreAuthorize("@ss.hasPermission('base:center-user:update')")
    public CommonResult<Boolean> updateCenterUser(@Valid @RequestBody CenterUserUpdateReqVO updateReqVO) {
        centerUserService.updateCenterUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工作中心人员绑定")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:center-user:delete')")
    public CommonResult<Boolean> deleteCenterUser(@RequestParam("id") Long id) {
        centerUserService.deleteCenterUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工作中心人员绑定")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:center-user:query')")
    public CommonResult<CenterUserRespVO> getCenterUser(@RequestParam("id") Long id) {
        CenterUserDO centerUser = centerUserService.getCenterUser(id);
        return success(CenterUserConvert.INSTANCE.convert(centerUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工作中心人员绑定列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:center-user:query')")
    public CommonResult<List<CenterUserRespVO>> getCenterUserList(@RequestParam("ids") Collection<Long> ids) {
        List<CenterUserDO> list = centerUserService.getCenterUserList(ids);
        return success(CenterUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工作中心人员绑定分页")
    @PreAuthorize("@ss.hasPermission('base:center-user:query')")
    public CommonResult<PageResult<CenterUserRespVO>> getCenterUserPage(@Valid CenterUserPageReqVO pageVO) {
        PageResult<CenterUserDO> pageResult = centerUserService.getCenterUserPage(pageVO);
        PageResult<CenterUserRespVO> resp = CenterUserConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(tmp -> {
            AdminUserRespDTO user = adminUserApi.getUser(tmp.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                tmp.setNickname(user.getNickname());
                tmp.setMobile(user.getMobile());
                DeptRespDTO dept = deptApi.getDept(user.getDeptId());
                if (ObjectUtil.isNotEmpty(dept)) {
                    tmp.setDeptName(dept.getName());
                }
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工作中心人员绑定 Excel")
    @PreAuthorize("@ss.hasPermission('base:center-user:export')")
    @OperateLog(type = EXPORT)
    public void exportCenterUserExcel(@Valid CenterUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CenterUserDO> list = centerUserService.getCenterUserList(exportReqVO);
        // 导出 Excel
        List<CenterUserExcelVO> datas = CenterUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工作中心人员绑定.xls", "数据", CenterUserExcelVO.class, datas);
    }

}
